﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using DAO;
using BUS;
public partial class Admin_Pages_WRAREAManager : BasePage
{
    public static bool Insert;
    BUS_WRAREA busWRAREA = new BUS_WRAREA();
    BUS_WRLANGUAGE busWRLANGUAGE = new BUS_WRLANGUAGE();
    private void Show(string Mess)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + Mess + "')", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            BindGrid();
            BindDrop();
        }
    }

    private void BindGrid()
    {
        grvWRAREA.DataSource = busWRAREA.Get_Area_List();
        //grvWRAREA.DataSource = busWRAREA._GetList().Select(p=>new
        //{
        //    p.ID,
        //    p.AreaName,
        //    p.AreaCode,
        //    p.Description,
        //    p.Keywords,
        //    p.Active,
        //    CreateDate = Convert.ToDateTime(p.CreateDate).ToShortDateString(),
        //    LangName = busWRLANGUAGE._GetObject(Convert.ToInt32(p.IDLang)).LangName
        //}).ToList();
        grvWRAREA.DataBind();
    }

    public void BindDrop()
    {
        dropIDLang.DataSource = busWRLANGUAGE._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, p.LangName }).ToList();
        dropIDLang.DataTextField = "LangName";
        dropIDLang.DataValueField = "Id";
        dropIDLang.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = true;
        pnShow.Visible = false;
        Insert = true;
        txtAreaName.Text = "";
        txtAreaCode.Text = "";
        txtDescription.Text = "";
        txtKeywords.Text = "";

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (grvWRAREA.Items.Count > 0)
        {
            for (int i = 0; i < grvWRAREA.Items.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvWRAREA.Items[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    string Id = grvWRAREA.Items[i].Cells[1].Text;
                    try
                    {
                        busWRAREA._Delete(Convert.ToInt32(Id));
                    }
                    catch
                    {
                    }
                }
            }
        }
        BindGrid();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (txtAreaName.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Area Name!");
            txtAreaName.Focus();
            return;
        }
        if (txtAreaCode.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Area Code!");
            txtAreaCode.Focus();
            return;
        }
        if (txtDescription.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Description!");
            txtDescription.Focus();
            return;
        }
        if (txtKeywords.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Keywords!");
            txtKeywords.Focus();
            return;
        }


        WRAREA obj = new WRAREA();
        obj.AreaName = txtAreaName.Text;
        obj.AreaCode = txtAreaCode.Text;
        obj.IDLang = Convert.ToInt32(dropIDLang.SelectedValue);
        obj.Description = txtDescription.Text;
        obj.UpdateDate = DateTime.Now;
        obj.IDUpdateBy = Convert.ToInt32(Session["userid"].ToString());
        obj.Active = dropActive.SelectedValue;
        obj.Keywords = txtKeywords.Text;
        if (Insert)
        {
            obj.IDCreateBy = Convert.ToInt32(Session["userid"].ToString());
            obj.CreateDate = DateTime.Now;
            busWRAREA._Add(obj);
        }
        else
        {
            obj.ID = Convert.ToInt32(txtID.Text);
            busWRAREA._Update(obj);
        }
        BindGrid();
        pnUpdate.Visible = false;
        pnShow.Visible = true;
        Insert = false;

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = false;
        pnShow.Visible = true;

    }

    protected void grvWRAREA_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string Id = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            WRAREA _WRAREA = busWRAREA._GetObject(Convert.ToInt32(Id));
            txtID.Text = Id;
            txtAreaName.Text = _WRAREA.AreaName;
            txtAreaCode.Text = _WRAREA.AreaCode;
            try
            {
                dropIDLang.SelectedValue = _WRAREA.IDLang.ToString();
            }
            catch
            {
                Show("Language is not active!");
            }
            txtDescription.Text = _WRAREA.Description;
            dropActive.SelectedIndex = _WRAREA.Active == "Y" ? 0 : 1;
            txtKeywords.Text = _WRAREA.Keywords;

            pnUpdate.Visible = true;
            pnShow.Visible = false;
        }
        else if (e.CommandName == "Delete")
        {
            busWRAREA._Delete(Convert.ToInt32(Id));
            BindGrid();
        }

    }

    protected void grvWRAREA_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grvWRAREA.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
}
